package com.yycx.module.bus.provider.handler.haiying.settle.pay;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import com.yycx.common.utils.RedisUtils;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.handler.haiying.BusinessUtils;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.bus.provider.service.hy.BusHyorderInoviceDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 结算支付申请单
 *
 * @author zyf
 */
@Component
public class HySettlePayPaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Resource
    public RedisUtils redisUtils;

    @Autowired
    private BusinessUtils businessUtils;

    @Autowired
    private BusPaymentService paymentService;

    @Resource
    private BusHyorderInoviceDetailsService orderInoviceDetailsService;

    @Resource
    private BusSalemanService salemanService;
    @Resource
    private BusDeptService deptService;


    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        Long companyId = OpenHelper.getCompanyId();
        payment.setPaymentId(IdWorker.getId());
        //结算明细
        String selectIds = cs.getParams("selectIds");
        Long settleApplyId = cs.getLong("settleApplyId");
        ApiAssert.isNotEmpty("settleApplyId参数为空", settleApplyId);
        ApiAssert.isNotEmpty("结算明细不能为空", selectIds);
        EntityMap totalSettleAmountMap = orderInoviceDetailsService.totalSettleAmount(selectIds);
        String drugType = totalSettleAmountMap.get("drugType");
        //结算金额
        BigDecimal amount = payment.getAmount();
        ApiAssert.isNotEmpty("结算金额不能为空", amount);
        Long userId = OpenHelper.getUserId();
        BusPayment settleApplyPayment = paymentService.getById(settleApplyId);
        ApiAssert.isNotEmpty("结算申请不能为空", settleApplyPayment);
        payment.setSettleType(settleApplyPayment.getSettleType());
        payment.setParentId(settleApplyId);
        String paymentNo = businessUtils.getPaymentNo(companyId, userId, AmountTypeEnum.Settle);
        payment.setPaymentNo(paymentNo);
        payment.setCompanyId(companyId);
        payment.setDrugType(drugType);
        payment.setUserId(userId);
        payment.setAmountDate(DateUtils.getNowDate());
        payment.setAmount(amount);
        payment.setNickName(OpenHelper.getNickName());
        payment.setOrganizationId(OpenHelper.getOrganizationId());
        payment.setTitle("结算支付申请单");
        payment.setPrintTag(CommonConstants.DISABLED);
        Long saleManId = cs.getLong("saleManId");
        BusSaleman saleman = salemanService.getById(saleManId);
        if (FlymeUtils.isNotEmpty(saleman)) {
            payment.setPayeeName(saleman.getSaleManName());
            payment.setSaleManCode(saleman.getSaleManBusinessNo());
        }
        Long deptId = cs.getLong("deptId");
        BusDept dept = deptService.getById(deptId);
        if (FlymeUtils.isNotEmpty(dept)) {
            payment.setDeptName(dept.getDeptName());
        }
        payment.setSelectIds(null);
        payment.setAmountType(AmountTypeEnum.Settle.getCode());
        payment.setPaymentState(CommonConstants.INT_0);
    }

    @Override
    public void prepare(CriteriaUpdate cu, EntityMap params, BusPayment payment) {

    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        String settleIds = cs.getParams("selectIds");
        orderInoviceDetailsService.updateSettlePayId(entity.getPaymentId(), settleIds);
    }
}
